package com.lb.shopguide.ui.fragment.boss;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;

import com.blankj.utilcode.util.ToastUtils;
import com.flyco.tablayout.SlidingTabLayout;
import com.lb.baselib.http.ResponseCode;
import com.lb.baselib.http.SimpleLbObserver;
import com.lb.baselib.http.bean.BaseResponse;
import com.lb.shopguide.R;
import com.lb.shopguide.adapter.viewpager.AdapterMerchantType;
import com.lb.shopguide.base.BaseMainFragment;
import com.lb.shopguide.config.UserConfigManager;
import com.lb.shopguide.http.method.ApiMethodBoss;
import com.lb.baselib.http.SimpleObserverOnNextListener;
import com.lb.shopguide.ui.activity.BossMainActivity;
import com.trello.rxlifecycle2.android.FragmentEvent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by kang on 2017/12/23.
 */

public class FragmentMerchant extends BaseMainFragment {
    @BindView(R.id.tabs)
    SlidingTabLayout mTabLayout;
    @BindView(R.id.viewPager)
    ViewPager mViewPager;

    private List<Fragment> mFragmentList;
    private List<String> titles;

    private AdapterMerchantType mAdapterMerchant;

    public static FragmentMerchant newInstance() {
        return new FragmentMerchant();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_merchant;
    }



    @Override
    public void initData() {
        mFragmentList = new ArrayList<>();
        titles = new ArrayList<>();
        mUserConfigManager = UserConfigManager.getInstance(mContext);
        initRequest();
    }

    @Override
    public void initView() {

        bindType();
    }

    @Override
    public void finishRefresh() {

    }

    @Override
    public void sessionOutOfDate() {
        ((BossMainActivity) getActivity()).onSessionOutOfDate();
    }

    @Override
    public void requestError(String error) {
        ToastUtils.showShort(error);
    }

    private void bindType() {
        initTitles();
        initFragment();

        mAdapterMerchant = new AdapterMerchantType(getChildFragmentManager(), mFragmentList, titles);
        mViewPager.setAdapter(mAdapterMerchant);
        mViewPager.setOffscreenPageLimit(4);
        mTabLayout.setViewPager(mViewPager);
    }

    private void initTitles() {
        titles.add("数据概览");
        titles.add("门店数据");
    }

    private void initRequest() {
        if (TextUtils.isEmpty(mUserConfigManager.getDistrictInfo()) && !TextUtils.isEmpty(mUserConfigManager.getUserToken())) {
            //区域信息
            sendRequestGetZoneInfo();
        }
    }

    private void initFragment() {
        mFragmentList.add(FragmentDataOverview.newInstance());
        mFragmentList.add(FragmentShopData.newInstance());
    }


    @Override
    public String getPageTag() {
        return FragmentMerchant.class.getName();
    }

    private void sendRequestGetZoneInfo() {
        SimpleObserverOnNextListener<BaseResponse> listener = new SimpleObserverOnNextListener<BaseResponse>() {

            @Override
            public void onNext(BaseResponse response) {
                if (response.getReturnCode() == ResponseCode.RESPONSE_SUCCESS) {
                    if (response.getReturnContent() != null) {
                        mUserConfigManager.setDistrictInfo(response.getReturnContent().toString());
                        mUserConfigManager.save2Sp(true);
                    }
                }
            }
        };
        ApiMethodBoss.getDistrictInfo(new SimpleLbObserver(listener,otherListener),null, FragmentEvent.DESTROY_VIEW);
    }
}
